# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def generate_html_for_file media
    case media.media_type_number
      when Media::TYPE_PICTURE
      image_tag url_for_file_column("media", "file")
      when Media::TYPE_AUDIOCLIP
      # FIXME: add MIME detection (and/or constrain file types to .mp3 and/or .wav) and change to :object (http://joliclic.free.fr/html/object-tag/en/object-audio.html)
      # Recommend we use ffmpeg or mpg321 or somethin
      content_tag :embed, nil, :src => url_for_file_column("media", "file")
      when Media::TYPE_VIDEO
      # TODO: Test this and/or do the same as TYPE_AUDIOCLIP
      content_tag :embed, nil, :src => url_for_file_column("media", "file")
    else
      raise "Unsupported media type"
    end
  end
  
  def display_categories(categories, parent_id, cur_level, max_level)
      categories = categories.sort_by {|d| d.name }
      if cur_level == 1
        ret = "<ul class=\"borderless\">"
      else
        ret = "<ul>" 
      end 
      if parent_id == nil && cur_level == 1
        ret << "<li><a href=\"/catalogue\">" + l(:common, 'all') + "</a></li>"
      elsif cur_level == 1
        ret << "<li class = \"folder\"><a class=\"submenu\" style=\"font-weight:bold\" href=\"/catalogue\">" + l(:common, 'all') + "</a>"
        ret << display_categories(@root_categories, nil, 2, 2) 
        ret << "</li>"
      end
      for category in categories
        if category.parent_id == parent_id
          ret << display_category(category, cur_level, max_level)
        end
      end
      ret << "</ul>" 
  end
  
  def display_category(category, cur_level, max_level)
    children = category.children.sort_by {|d| d.name }
    ret = ""
    if children.length > 0 && cur_level < max_level
      ret << "<li class = \"folder\">"
      ret << link_to(h(category.name(get_hr_language)), {:controller => "categories", :action => "show", :id => category}, :class => "submenu")
    else 
      ret << "<li>"
      ret << link_to(h(category.name(get_hr_language)), :controller => "categories", :action => "show", :id => category)
    end
    cur_level += 1
    ret << display_categories(children, category.id, cur_level, max_level) if category.children.any? && cur_level <= max_level 
    ret << "</li>" 
  end
  
  # Deprecated, use jQuery
  def add_artwork_link(text, artwork)
    link_to_remote(text, {:update => "cart_total_items", :url => {:controller => "cart", :action => "add", :id => artwork}},
      {:title => "Add to Cart", :href => url_for( :controller => "cart", :action => "add", :id => artwork), :id => "add_to_cart_button"})
  end
 
  def remove_artwork_link(text, artwork)
    link_to_remote text, {:update => "cart_total_items", :url => {:controller => "cart", :action => "remove", :id => artwork}},
    {:title => "Remove from Cart",
     :href => url_for( :controller => "cart", :action => "remove", :id => artwork)}
  end
 
  def clear_cart_link(text = "Clear Cart")
    link_to_remote text, {:update =>"cart_total_items", :url => {:controller => "cart", :action => "clear" }},
                         {:href => url_for(:controller => "cart", :action => "clear")}
  end
  
  def get_hr_language
    HttpAcceptLanguageMap[@lang.to_sym]
  end
  
  # Fixed from actionpack because Ruby core developers are retarded (1.8.7 breaks this)
  def truncate(text, length = 30, truncate_string = "...")
    if text.nil? then return end
    l = length - truncate_string.length
    
    if(text.length > length)
      cutoff = text[0...l].rindex(/[ .,?!;:]/)
      text[0...cutoff] + truncate_string
    else
      text
    end
  end
  
end
